/*
 * Team Name: Team Sasquatch
 * Cody Sharff
 * Anthony Rios
 * Prince Kishore
 * Travis Drake
 */

package model;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

/**
 * This class represents an auction that contains
 * information about the auction such as ID, name,
 * NPO, intake person, auction date, duration, start
 * time and comments.
 * 
 * @author Cody Sharff
 * @author Modified by Prince Kishore
 * @author Modified by Travis (addItem)
 *
 */
public class Auction {

	/**
	 * The list of items in an auction.
	 */
	List<Item> my_items = new ArrayList<Item>();

	/**
	 * The auction ID. Increased by one when
	 * a auction is created.
	 */
	public static int next_auction_id = 1;

	/**
	 * The auction ID.
	 */
	private int my_auction_id;


	/**
	 * The name of the auction is the combination of the NPO and
	 * the date.
	 */
	private String my_auction_name;

	/**
	 * The non profit organization.
	 */
	private String my_npo;

	/**
	 * The contact person of the auction.
	 */
	private String my_contact_person;

	/**
	 * The contact number of the contact person.
	 * Format: ###-###-####
	 */
	private long my_contact_number;

	/**
	 * The intake person of the auction.
	 */
	private String my_intake_person;

	/**
	 * The auction date.
	 */
	private Calendar my_auction_date;

	/**
	 * The duration of the auction, in minutes.
	 */
	private int my_auction_duration;

	/**
	 * The comments of the auction.
	 */
	private String my_comments;

	/**
	 * Format the Calendar object in the form of Jan 12, 1952.
	 */
	private static final DateFormat date_format = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);


	/**
	 * Constructs an auction object.
	 * 
	 */
	public Auction(int the_auction_id_number, String the_npo, String the_contact_person, long the_contact_number,
			String the_intake_person, Calendar the_auction_date, int the_auction_duration, String the_comments) {
		my_auction_id = the_auction_id_number;
		my_npo = the_npo;
		my_contact_person = the_contact_person;
		my_contact_number = the_contact_number;
		my_intake_person = the_intake_person;
		my_auction_date = the_auction_date;
		my_auction_duration = the_auction_duration;
		my_comments = the_comments;
		my_auction_name = the_npo + " - " + date_format.format(the_auction_date.getTime());

	}

	/**
	 * Adds an item to the auction.
	 * 
	 * @before item_name != null
	 * @before the_quantity >= 1
	 * @before the_min_start_bid >= 1.00
	 * @before the_storage != null
	 * @before the_condition != null
	 * @after my_items.size() == \old my_items.size() + 1
	 * @after Item.next_item_id == \old Item.next_item_id + 1
	 * 
	 */
	public void addItem(String the_item_name, 
			int the_quantity, int the_min_start_bid, String the_donor, int the_size,
			String the_storage, Condition the_condition, String the_comment) {

		my_items.add(new Item(Item.next_item_id, the_item_name, the_quantity,
				the_min_start_bid, the_donor, the_size, the_storage, the_condition, the_comment));
		Item.next_item_id++;
		
	}

	/**
	 * Adds a pre-existing item to an auction, used for loading and unloading from program.
	 * @author Travis Drake
	 * 
	 * @before the_item != null
	 * @after my_items = \old my_items + the_item
	 */
	public void addItem(Item the_item) {        
		my_items.add(the_item);
	}

	/**
	 * Returns the auction's ID.
	 * 
	 * @ensures \result = my_auction_id
	 */
	public int getAuctionID() {
		return my_auction_id;
	}

	/**
	 * Returns the NPO associated with the auction.
	 * 
	 * @ensures \result = my_npo
	 */
	public String getNPO() {
		return my_npo;
	}

	/**
	 * Returns the contact person associated with the auction.
	 */
	public String getContactPerson() {
		return my_contact_person;
	}

	/**
	 * Returns the contact persons phone number.
	 */
	public long getContactNumber() {
		return my_contact_number;
	}

	/**
	 * Returns the auction central intake person that
	 * added the auction to the database.
	 */
	public String getACIntake() {
		return my_intake_person;
	}

	/**
	 * Returns the date that the auction is assigned to.
	 */
	public Calendar getDate() {
		return my_auction_date;
	}

	/**
	 * Returns the duration (in minutes) of the auction.
	 */
	public int getDuration() {
		return my_auction_duration;
	}

	/**
	 * Returns any comments attached to the auction.
	 */
	public String getComments() {
		return my_comments;
	}

	/**
	 * Returns a list of the items associated with
	 * the auction.
	 */
	public List<Item> getItems() {
		return my_items;
	}

	/**
	 * Sets the next auction ID so when a new auction
	 * is created it will start a specific ID.
	 */
	public static void setNextID(int the_id) {
		next_auction_id = the_id;
	}

	/**
	 * Assigns the NPO associated with the auction.
	 */
	public void setNPO(String the_npo) {
		my_npo = the_npo;
	}

	/**
	 * Assigns the contact person associated with the auction.
	 */
	public void setContactPerson(String the_contact_person) {
		my_contact_person = the_contact_person;
	}

	/**
	 * Assigns the contact persons phone number.
	 */
	public void setContactNumber(long the_contact_number) {
		my_contact_number = the_contact_number;
	}

	/**
	 * Assigns an AuctionCentral intake person to the auction.
	 */
	public void setACIntake(String the_intake_person) {
		my_intake_person = the_intake_person;
	}

	/**
	 * Sets the auctions date.
	 */
	public void setDate(Calendar the_auction_date) {
		my_auction_date = the_auction_date;
	}

	/**
	 * Assigns the duration of the auction.
	 */
	public void setDuration(int the_auction_duration) {
		my_auction_duration = the_auction_duration;
	}

	/**
	 * Sets any comments associated with the comments.
	 */
	public void setComments(String the_comments) {
		my_comments = the_comments;
	}

	/**
	 * Returns a list of the items associated with
	 * the auction.
	 * 
	 * @requires the_item_id >= 1
	 * @ensures !my_items.contains(the_item_id)
	 */
	public void removeItem(int the_item_id) {
		my_items.remove(the_item_id);
	}

	/**
	 * Returns a String representation of the item.
	 *
	 *@return a String including the item name,price, and if the item is bulk 
	 *            it prints the items bulk price and quantity needed to get bulk price.
	 */
	@Override
	public String toString() {
		return my_auction_name;
	}


	// Comparators

	/**
	 * A comparator used to compare auctions according to the dates.
	 * 
	 * @requres a1 != null
	 * @requires a2 != null
	 * @ensures an integer 
	 */
	public static final Comparator<Auction> DATE_ASC
	= new Comparator<Auction>() {
		@Override
		public int compare(Auction a1, Auction a2) {
			return a1.getDate().compareTo(a2.getDate());
		}
	};

	/**
	 * A comparator used to compare auctions according to the auction ID.
	 * 
	 * @requires a1 !=null
	 * @requires a2 !=null
	 * @ensures an Integer
	 */
	public static final Comparator<Auction> ID_ASC
	= new Comparator<Auction>() {
		@Override
		public int compare(Auction a1, Auction a2) {
			Integer i1 = a1.getAuctionID();
			Integer i2 = a2.getAuctionID();
			return i1.compareTo(i2);
		}
	};  

	/**
	 * A comparator used to compare auctions according to the auction number of items.
	 * 
	 * @requires a1 !=null
	 * @requires a2 !=null
	 * @ensures an Integer
	 */
	public static final Comparator<Auction> NUM_ITEMS_ASC
	= new Comparator<Auction>() {
		@Override
		public int compare(Auction a1, Auction a2) {
			return ((Integer) a1.getItems().size()).compareTo((Integer) a2.getItems().size());
		}
	}; 

	/**
	 * A comparator used to compare auctions according to the auction start time.
	 * 
	 * @requires a1 !=null
	 * @requires a2 !=null
	 * @ensures an Integer
	 */
	public static final Comparator<Auction> START_TIME_ASC
	= new Comparator<Auction>() {
		@Override
		public int compare(Auction a1, Auction a2) {
			return a1.getDate().compareTo(a2.getDate());
		}
	};  

	/**
	 * A comparator used to compare auctions according to the auction duration.
	 * 
	 * @requires a1 !=null
	 * @requires a2 !=null
	 * @ensures an Integer
	 */
	public static final Comparator<Auction> DURATION_ASC
	= new Comparator<Auction>() {
		@Override
		public int compare(Auction a1, Auction a2) {
			return ((Integer) a1.getDuration()).compareTo((Integer) a2.getDuration());
		}
	};
}
